package com.tj.jinzao.widgets;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.support.v4.graphics.ColorUtils;
import android.util.AttributeSet;
import android.view.View;

import com.tj.jinzao.R;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.utils.ImageUtils;


/**
 * 首页健康度
 * Created by wjy on 2018/10/11.
 */
public class HealthProgressBar extends View {

    private Paint mPaintBg, mTextPaint;
    private int[] mLines;
    private Bitmap mCircle;
    private Bitmap mPoint;
    private int mCircleHeight, mPointHeight;
    private int mLineColor;
    private boolean mUp;

    private float mMaxLine = 0;

    private PaintFlagsDrawFilter mPaintFlagsDrawFilter;

    public HealthProgressBar(Context context) {
        super(context);
        init();
    }

    public HealthProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }


    public void init() {
        mPaintFlagsDrawFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
        //初始化坐标
        mLineColor = Color.parseColor("#FFCCCCCC");
        mPaintBg = new Paint();
        mPaintBg.setStyle(Paint.Style.FILL);
        mPaintBg.setColor(mLineColor);
        mPaintBg.setAntiAlias(true);
        //先创建一个渲染器
        mTextPaint = new Paint();
        mTextPaint.setColor(Color.parseColor("#FF00C08D"));
        mTextPaint.setTextSize(20 * Constant.SCALE_X);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        int measureHeight = Constant.SCREEN_HEIGHT * 273 / 667;
        int space = (int) (30 * Constant.SCALE_X);
        mCircleHeight = Math.min(measureHeight ,Constant.SCREEN_WIDTH * 4 / 5);
        mCircle = ImageUtils.scaleBitmap(ImageUtils.getBitmap(getResources(), R.mipmap.main_circle)
                ,mCircleHeight - (int)(space * 2.5f) ,mCircleHeight- (int)(space * 2.5f));
        mPoint = ImageUtils.scaleBitmap(ImageUtils.getBitmap(getResources(), R.mipmap.main_scan)
                , mCircle.getWidth(), mCircle.getWidth() * 19 / 438);
        mLines = new int[3];
        mLines[0] = mCircle.getWidth() / 2;
        mLines[1] = mCircle.getWidth() / 2 - space;
        mLines[2] = mCircle.getWidth() / 2 - space * 2;
        mMaxLine = space * 2 + mCircle.getWidth() / 2;

    }

    @Override
    protected void onDraw(Canvas c) {
        c.setDrawFilter(mPaintFlagsDrawFilter);
        //圆环
        int centerX = getWidth() / 2;
        int centerY = getHeight() / 2;
        for (int i = 0; i < mLines.length; i++) {
            float rate = (mMaxLine - mLines[i]) / mMaxLine * 3.7f;
            mPaintBg.setColor(ColorUtils.blendARGB(Color.WHITE, mLineColor, rate));
//            if (i == 0) mPaintBg.setColor(ColorUtils.blendARGB(Color.WHITE, Color.RED, rate));
//            else if (i == 1) mPaintBg.setColor(ColorUtils.blendARGB(Color.WHITE, Color.BLUE, rate));
//            else if (i == 2)
//                mPaintBg.setColor(ColorUtils.blendARGB(Color.WHITE, Color.GREEN, rate));
            mPaintBg.setAlpha((int) (255 * rate));
            c.drawCircle(centerX, centerY, mLines[i], mPaintBg);
        }
        mPaintBg.setAlpha(255);
        c.drawBitmap(mCircle, centerX - mCircle.getWidth() / 2, centerY - mCircle.getHeight() / 2, mPaintBg);
        c.drawText("体质检测", centerX, centerY + mTextPaint.getTextSize() / 2.3f, mTextPaint);
        c.drawBitmap(mPoint, centerX - mPoint.getWidth() / 2, mPointHeight, mPaintBg);

        onUpdate();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mPointHeight = mCircleHeight / 2;
        setMeasuredDimension(Constant.SCREEN_WIDTH, mCircleHeight);
    }

    private void onUpdate() {
        if (mUp) {
            mPointHeight -= 3;
            if (mPointHeight <= (getHeight() - mCircle.getHeight()) / 2) {
                mUp = false;
            }
        } else {
            mPointHeight += 3;
            if (mPointHeight >= (getHeight() + mCircle.getHeight()) / 2 - mPoint.getHeight()) {
                mUp = true;
            }
        }
        for (int i = 0; i < mLines.length; i++) {
            mLines[i] += 2;
            if (mLines[i] >= mMaxLine) {
                mLines[i] = mCircle.getWidth() / 2 - (int) (30 * Constant.SCALE_X);
            }
        }
        postInvalidateDelayed(20);

    }

    public void onDestroy() {
        if (mCircle != null && !mCircle.isRecycled()) {
            mCircle.recycle();
        }
        if (mPoint != null && !mPoint.isRecycled()) {
            mPoint.recycle();
        }
    }


}
